package com.cbg.es.controller;

import com.cbg.dto.SelectedColListDTO;
import com.cbg.es.dto.EsCategoryDTO;
import com.cbg.es.dto.EsTableListDTO;
import com.cbg.es.service.IEsService;
import com.cbg.es.vo.CategoryVO;
import com.cbg.es.vo.EsTableCategoryVO;
import com.cbg.es.vo.TableCategoryVO;
import com.cbg.es.vo.TableColVO;
import com.cbg.vo.CategoryNameVO;
import com.cbg.vo.DirectionsVO;
import com.shuyuwl.core.support.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 全景通查Controller
 *
 * @author wangjuzheng
 * @since 2018-05-31
 */
@RestController
@Api(tags = "全景通查-搜索配置")
@RequestMapping("/api/es")
@CrossOrigin(allowCredentials = "true", maxAge = 3600, allowedHeaders = "*", origins = "*")
public class EsController {

    @Resource
    private IEsService esService;

    /**
     * 获取分类信息
     *
     * @param type 分类区分（0分类和表树信息 1分类信息）
     * @return 模型
     */
    @GetMapping(value = "/category/list/{type}")
    @ApiOperation(value = "获取分类信息")
    public ServerResponse<List<EsCategoryDTO>> getCategoryList(@ApiParam(value = "分类区分（0分类和表树信息 1分类信息）")
                                                               @PathVariable Integer type) {

        //获取分类信息
        List<EsCategoryDTO> list = esService.getEsCategoryDTOs(type);
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 分类位置的移动
     *
     * @param directionsVO 分类移动VO
     * @return 模型
     */
    @PutMapping(value = "/category/directions")
    @ApiOperation(value = "分类位置的移动")
    public ServerResponse updateCategoryDirections(@Validated @RequestBody DirectionsVO directionsVO) {

        //分类位置的移动
        esService.updateCategoryDirections(directionsVO);
        return ServerResponse.createBySuccess("移动成功");
    }

    /**
     * 分类名称的修改
     *
     * @param categoryNameVO 更新名称vo
     * @return 修改后的分类信息
     */
    @PutMapping(value = "/category/name")
    @ApiOperation(value = "分类名称的修改")
    public ServerResponse<EsCategoryDTO> updateCategoryName(@Validated @RequestBody CategoryNameVO categoryNameVO) {

        //分类名称的修改
        EsCategoryDTO esCategoryDTO = esService.updateCategoryName(categoryNameVO);
        return ServerResponse.createBySuccess("保存成功", esCategoryDTO);
    }

    /**
     * 分类的添加
     *
     * @param categoryVO 分类添加模型
     * @return 添加的分类信息
     */
    @PostMapping(value = "/category")
    @ApiOperation(value = "分类的添加")
    public ServerResponse<EsCategoryDTO> addCategory(@Validated @RequestBody CategoryVO categoryVO) {

        //分类的添加
        EsCategoryDTO esCategoryDTO = esService.addCategory(categoryVO);
        return ServerResponse.createBySuccess("操作成功", esCategoryDTO);
    }

    /**
     * 分类的删除
     *
     * @param categoryId 分类id
     * @return 模型
     */
    @DeleteMapping(value = "/category/{categoryId}")
    @ApiOperation(value = "分类的删除")
    public ServerResponse deleteCategory(@ApiParam(value = "分类id") @PathVariable Long categoryId) {

        esService.deleteCategory(categoryId);
        return ServerResponse.createBySuccess("删除成功");
    }

    /**
     * 分类添加表
     *
     * @param tableCategoryVO 全景通查添加表信息VO
     * @return 模型
     */
    @PostMapping(value = "/table-category")
    @ApiOperation(value = "分类添加表")
    public ServerResponse addEsTableCategory(@Validated @RequestBody TableCategoryVO tableCategoryVO) {

        //分类添加表
        esService.addEsTableCategory(tableCategoryVO.getCategoryId(), tableCategoryVO.getIds());
        return ServerResponse.createBySuccess("操作成功");
    }

    /**
     * 表的更新
     *
     * @param esTableCategoryVO 展示表保存VO
     * @return 模型
     */
    @PutMapping(value = "/table-category")
    @ApiOperation(value = "表的更新")
    public ServerResponse saveEsTableCategory(@Validated @RequestBody EsTableCategoryVO esTableCategoryVO) {

        esService.saveEsTableCategory(esTableCategoryVO.getCategoryId(), esTableCategoryVO.getList());
        return ServerResponse.createBySuccess("操作成功");
    }

    /**
     * 所选分类的表信息
     *
     * @param categoryId 父级id
     * @return 表信息
     */
    @GetMapping(value = "/table-category/selected/list/{categoryId}")
    @ApiOperation(value = "所选分类的表信息")
    public ServerResponse<List<EsTableListDTO>> selectedCategory(@ApiParam(value = "分类id") @PathVariable Long categoryId) {

        //所选分类的表信息
        List<EsTableListDTO> list = esService.selectedCategory(categoryId);
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 获取没有选中的表信息
     *
     * @param categoryId 分类id
     * @return 表信息
     */
    @GetMapping(value = "/table/list/{categoryId}")
    @ApiOperation(value = "获取没有选中的表信息")
    public ServerResponse<List<EsTableListDTO>> getTableCategoryList(@ApiParam(value = "分类id") @PathVariable Long categoryId) {

        //获取没有选中的表信息
        List<EsTableListDTO> list = esService.getEsTables(categoryId);
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 获取列信息
     *
     * @param tableCategoryIdStr 表id字符串
     * @return 列信息
     */
    @GetMapping(value = "/table-col/list/{tableCategoryIdStr}")
    @ApiOperation(value = "获取列信息")
    public ServerResponse<List<SelectedColListDTO>> selectedTableCategory(@ApiParam(value = "父级id") @PathVariable String tableCategoryIdStr) {

        //把表id字符串转换位表id
        Long tableCategoryId = esService.formatCategoryTableId(tableCategoryIdStr);
        //根据展示关系表id获取列信息
        List<SelectedColListDTO> listDTOS = esService.selectedTableCategory(tableCategoryId);
        return ServerResponse.createBySuccess(listDTOS);
    }

    /**
     * 列的更新
     *
     * @param tableColVO 更新列的VO
     * @return 模型
     */
    @PutMapping(value = "/table-col")
    @ApiOperation(value = "列的更新")
    public ServerResponse saveEsTableCol(@Validated @RequestBody TableColVO tableColVO) {

        //把表id字符串转换位表id
        Long tableCategoryId = esService.formatCategoryTableId(tableColVO.getTableCategoryId());
        esService.saveEsTableCol(tableCategoryId, tableColVO.getList());
        return ServerResponse.createBySuccess("操作成功");
    }

}
